using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Dynamic;
using System.Data.SqlClient;
using System.Configuration;
using Model;
using Common;

namespace DAL
{
    public class PriceDAL
    {
        public static int Save(PriceInfo info)
        {
            string sql = string.Format("insert into [price]([ProductID],[OPrice],[APrice],[PPrice],[Unit],[SellCount],[LibraryCount]) values(@ProductID,@OPrice,@APrice,@PPrice,@Unit,@SellCount,@LibraryCount);select @@identity;");
            SqlParameter[] paras = new SqlParameter[]{
                new SqlParameter("@ProductID", info.ProductID),
                new SqlParameter("@OPrice", info.OPrice),
                new SqlParameter("@APrice", info.APrice),
                new SqlParameter("@PPrice", info.PPrice),
                new SqlParameter("@Unit", info.Unit),
                new SqlParameter("@SellCount", info.SellCount),
                new SqlParameter("@LibraryCount", info.LibraryCount)
            };
            return DataConvert.ToInt(CommonDAL.ExecuteScalar(sql, paras));
        }
        public static int Update(PriceInfo info)
        {
            string sql = string.Format("update [price] set [ProductID]=@ProductID,[OPrice]=@OPrice,[APrice]=@APrice,[PPrice]=@PPrice,[Unit]=@Unit,[SellCount]=@SellCount,[LibraryCount]=@LibraryCount where [ID]=@ID");
            SqlParameter[] paras = new SqlParameter[]{
                new SqlParameter("@ProductID", info.ProductID),
                new SqlParameter("@OPrice", info.OPrice),
                new SqlParameter("@APrice", info.APrice),
                new SqlParameter("@PPrice", info.PPrice),
                new SqlParameter("@Unit", info.Unit),
                new SqlParameter("@SellCount", info.SellCount),
                new SqlParameter("@LibraryCount", info.LibraryCount),
                new SqlParameter("@ID", info.ID)
            };
            return CommonDAL.ExecuteNoneQuery(sql, paras) ? info.ID : 0;
        }
        public static PriceInfo FindOneByID(int id)
        {
            DataRow dr = CommonDAL.FindOneByID(id, "[price]");
            return SimpleData(dr);
        }
        public static List<PriceInfo> FindListByIDs(string ids)
        {
            DataTable dt = CommonDAL.FindListByIDs(ids, "[price]");
            return ListSimpleData(dt);
        }
        public static List<PriceInfo> FindPage(string keyword, string begindate, string enddate, int startindex, int pagesize, string order, string desc, out int count)
        {
            StringBuilder filter = new StringBuilder();
            if (!string.IsNullOrEmpty(keyword))
            {
                filter.AppendFormat(" and [price].Name like '%{0}%'", keyword);
            }
            if (!string.IsNullOrEmpty(begindate) && !string.IsNullOrEmpty(enddate))
            {
                filter.AppendFormat(" and [price].CreateDate between '{0}' and '{1}'", DataConvert.ToDateTime(begindate).ToString("yyyy-MM-dd 00:00:00"), DataConvert.ToDateTime(enddate).ToString("yyyy-MM-dd 23:59:59"));
            }
            DataTable dt = CommonDAL.FindPage(startindex, pagesize, order, desc, filter, "[price]", out count);
            return ListSimpleData(dt);
        }
        public static List<PriceInfo> FindAll()
        {
            DataTable dt = CommonDAL.GetDataTable(string.Format("select * from [price]"));
            return ListSimpleData(dt);
        }
        public static List<PriceInfo> FindListByProductID(int productid)
        {
            DataTable dt = CommonDAL.GetDataTable(string.Format("select * from [price] where ProductID={0}",productid));
            return ListSimpleData(dt);
        }
        public static bool DeleteByProductID(int productid)
        {
            return CommonDAL.ExecuteNoneQuery(string.Format("delete from [price] where ProductID={0}", productid));
        }
        private static PriceInfo SimpleData(DataRow dr)
        {
            PriceInfo info = new PriceInfo();
            if (dr != null)
            {
                info.ID = DataConvert.ToInt(dr["ID"]);
                info.ProductID = DataConvert.ToInt(dr["ProductID"]);
                info.OPrice = DataConvert.ToFloat(dr["OPrice"]);
                info.APrice = DataConvert.ToFloat(dr["APrice"]);
                info.PPrice = DataConvert.ToFloat(dr["PPrice"]);
                info.Unit = DataConvert.ToString(dr["Unit"]);
                info.SellCount = DataConvert.ToInt(dr["SellCount"]);
                info.LibraryCount = DataConvert.ToInt(dr["LibraryCount"]);
            }
            return info;
        }
        private static PriceInfo AllData(DataRow dr)
        {
            PriceInfo info = new PriceInfo();
            if (dr != null)
            {
                info.ID = DataConvert.ToInt(dr["ID"]);
                info.ProductID = DataConvert.ToInt(dr["ProductID"]);
                info.OPrice = DataConvert.ToFloat(dr["OPrice"]);
                info.APrice = DataConvert.ToFloat(dr["APrice"]);
                info.PPrice = DataConvert.ToFloat(dr["PPrice"]);
                info.Unit = DataConvert.ToString(dr["Unit"]);
                info.SellCount = DataConvert.ToInt(dr["SellCount"]);
                info.LibraryCount = DataConvert.ToInt(dr["LibraryCount"]);
            }
            return info;
        }
        private static List<PriceInfo> ListSimpleData(DataTable dt)
        {
            List<PriceInfo> list = new List<PriceInfo>();
            foreach (DataRow row in dt.Rows)
            {
                list.Add(SimpleData(row));
            }
            return list;
        }
        private static List<PriceInfo> ListAllData(DataTable dt)
        {
            List<PriceInfo> list = new List<PriceInfo>();
            foreach (DataRow row in dt.Rows)
            {
                list.Add(AllData(row));
            }
            return list;
        }
    }
}

